package com.ynsj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.xdevapi.UpdateParams;
import com.ynsj.domain.User;
import com.ynsj.domain.UserAuthInfo;
import com.ynsj.model.*;
import com.ynsj.service.UserService;
import com.ynsj.vo.UserAuthInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Collections;

@RestController
@RequestMapping("/users")
@Api(tags = "会员的控制器")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    @ApiOperation("分页查询会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "页大小"),
            @ApiImplicitParam(name = "mobile",value = "手机号"),
            @ApiImplicitParam(name = "userId",value = "会员Id,精确查询"),
            @ApiImplicitParam(name = "userName",value = "会员名称"),
            @ApiImplicitParam(name = "realName",value = "’会员真实姓名"),
            @ApiImplicitParam(name = "status",value = "会员状态")
    })
    @PreAuthorize("hasAuthority('user_query')")
    public R<Page<User>> findUserPage(@ApiIgnore Page<User> userPage, String mobile, Long userId, String userName, String realName, Integer status){
        return R.ok(userService.findByPage(userPage,mobile,userId,userName,realName,status));
    }

    @PatchMapping
    @ApiOperation("修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "会员的json")
    })
    @PreAuthorize("hasAuthority('user_update')")
    public R updateUser(@RequestBody @Validated User user){
        boolean isOk = userService.updateById(user);
        if (isOk){
            return R.ok("更新会员信息成功");
        }
        return R.fail("更新会员信息失败");
    }

    @PostMapping("/status")
    @ApiOperation("启用/禁用会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "会员的id"),
            @ApiImplicitParam(name = "status",value = "会员状态")
    })
    @PreAuthorize("hasAuthority('user_update')")
    public R updateUserStatus(Long id,Integer status){
        boolean isOk = userService.updateStatus(id,status);
        if (isOk){
            return R.ok("启用/禁用会员成功");
        }
        return R.fail("启用/禁用会员失败");
    }

    @GetMapping("/info")
    @ApiOperation("根据id查询会员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "会员id")
    })
    @PreAuthorize("hasAuthority('user_query')")
    public R<User> findUserById(Long id){
        return R.ok(userService.getById(id));
    }

    @GetMapping("/directInvites")
    @ApiOperation("分页查询会员用户邀请列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "页大小"),
            @ApiImplicitParam(name = "userId",value = "会员Id,精确查询")
    })
    @PreAuthorize("hasAuthority('user_query')")
    public R<Page<User>> findDirectPage(@ApiIgnore Page<User> userPage, Long userId){
        return R.ok(userService.findDirectPage(userPage,userId));
    }

    @GetMapping("/auths")
    @ApiOperation("分页查询会员实名审核信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "页大小"),
            @ApiImplicitParam(name = "mobile",value = "手机号"),
            @ApiImplicitParam(name = "userId",value = "会员Id,精确查询"),
            @ApiImplicitParam(name = "userName",value = "会员名称"),
            @ApiImplicitParam(name = "realName",value = "’会员真实姓名"),
            @ApiImplicitParam(name = "status",value = "会员状态")
    })
    @PreAuthorize("hasAuthority('user_query')")
    public R<Page<User>> findAuthsPage(@ApiIgnore Page<User> userPage, String mobile, Long userId, String userName, String realName, Integer reviewsStatus){
        return R.ok(userService.findAuthByPage(userPage,mobile,userId,userName,realName,reviewsStatus));
    }

    @GetMapping("/auth/info")
    @ApiOperation("根据id查询用户认证详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "会员id")
    })
    public R<UserAuthInfoVo> findUserInfoById(Long id){
        return R.ok(userService.getUserInfoById(id));
    }

    @PostMapping("/auths/status")
    @ApiOperation("审核用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id"),
            @ApiImplicitParam(name = "authStatus",value = "用户审核状态"),
            @ApiImplicitParam(name = "authCode",value = "用户照片认证码"),
            @ApiImplicitParam(name = "remark",value = "拒绝通过原因")
    })
    public R updateAuthStatus(@RequestParam(required = true) Long id, @RequestParam(required = true) Integer authStatus, @RequestParam(required = true) Long authCode, String remark){
        userService.updateAuthStatus(id,authStatus,authCode,remark);
        return R.ok();

    }

    @GetMapping("/current/info")
    @ApiOperation("获取当前登录用户的对象信息")
    public R<User> currentUserInfo(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        return R.ok(userService.getById(Long.valueOf(userId)));
    }

    @PostMapping("/authAccount")
    @ApiOperation("用户实名认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "",value = "")
    })
    public R identifyCheck(@RequestBody UserCheckForm userCheckForm){
       boolean isOk = userService.identifyCheck(userCheckForm);
       if (isOk){
           return R.ok("认证成功");
       }
       return R.fail("认证失败");
    }

    @PostMapping("/authUser")
    @ApiOperation("用户进行高级认证")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "imgs",value = "用户认证图片")
    })
    public R authUser(@RequestBody String[] imgs){
        userService.authUser(Arrays.asList(imgs));
        return R.ok();
    }

    @PostMapping("/updatePhone")
    @ApiOperation("修改手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updatePhoneParam",value = "updatePhoneParam的json数据")
    })
    public R updatePhone(@RequestBody UpdatePhoneParam updatePhoneParam){
        boolean isOk = userService.updatePhone(updatePhoneParam);
        if (isOk){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    @GetMapping("/checkTel")
    @ApiOperation("检查新的手机号是否可用，若可用，给新手机号发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile",value = "新的手机号"),
            @ApiImplicitParam(name = "countryCode",value = "国家代码")
    })
    public R checkTel(@RequestParam(required = true) String mobile,@RequestParam(required = true) String countryCode){
        boolean isOk = userService.checkTel(mobile,countryCode);
        if (isOk){
            return R.ok("新手机校验成功");
        }
        return R.fail("新手机校验失败");
    }

    @PostMapping("/updateLoginPassword")
    @ApiOperation(value = "修改用户的登录密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updateLoginParam", value = "修改用户的登录密码")
    })
    public R updateLoginPwd(@RequestBody @Validated UpdateLoginParam updateLoginParam) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean isOk = userService.updateUserLoginPwd(userId, updateLoginParam);
        if (isOk) {
            return R.ok();
        }
        return R.fail("修改失败");
    }

    @PostMapping("/updatePayPassword")
    @ApiOperation(value = "修改用户的交易密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updateLoginParam", value = "修改用户的交易密码")
    })
    public R updatePayPwd(@RequestBody @Validated UpdateLoginParam updateLoginParam) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean isOk = userService.updateUserPayPwd(userId, updateLoginParam);
        if (isOk) {
            return R.ok();
        }
        return R.fail("修改失败");
    }

    @PostMapping("/setPayPassword")
    @ApiOperation(value = "重新设置交易密码")
    public R setPayPassword(@RequestBody @Validated UnsetPayPasswordParam unsetPayPasswordParam) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean isOk = userService.unsetPayPassword(userId, unsetPayPasswordParam);
        if (isOk) {
            return R.ok();
        }
        return R.fail("重置失败");
    }
}
